<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="./dist/qrcode.js"></script>
  <script src="./dist/html2canvas.js"></script>
  <script src="./dist/jspdf.js"></script>
  <title>B类证书</title>
  <style>
    @media print {
      /* 设置横向打印 */


      /* 设置纵向打印 */
      @page {
        size: portrait;
      }
    }

    body {
      background-color: #fff;
      padding: 36px;
    }

    .bg {
      position: relative;
      padding: 0px;
      background-color: #fff;
      width: 1080px;
      height: 1431px;
      background: url(./dist/b.jpg) center / 100% no-repeat;
      margin: 0 auto;
      font-family: 'SimSun', sans-serif;
    }

    .absolute {
      position: absolute;
    }

    .ID-box {
      top: 540.5px;
      left: 50%;
      transform: translate(-50%);
      margin-left: 26px;
      width: 446px;
      font-size: 22px;
      font-weight: 100;
    }

    .user-img {
      z-index: 999;
      width: 124px;
      height: 180px;
      background: center / 100% no-repeat;
      left: 402px;
      top: -10px;
    }

    .user-name {
      left: 0;
    }

    .user-gender {
      top: 41px;
      left: 0;
    }

    .user-idCard {
      top: 88px;
      left: 0;
    }

    .certificate-number {
      top: 129px;
      left: 0;
    }

    .company {
      top: 174px;
      left: 0;
    }

    .post {
      top: 218px;
      left: 0;
    }

    .technical-titles {
      top: 260px;
      left: 0;
    }

    .effective-date {
      top: 300px;
      left: 0;
      color: #000;
      font-weight: 200;
    }

    .init-date {
      position: absolute;
      top: 506px;
      left: 337px;
      font-size: 22px;
      width: 300px;
      color: #000;
      font-weight: 200;
    }

    .qr {
      position: absolute;
      top: 588px;
      left: -150px;
      font-size: 22px;
      width: 150px;
      height: 150px;
      background: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    #exportButton {
      margin: 10px;
      padding: 8px 16px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      position: absolute;
      top: 24px;
      right: 24px;
    }

    #exportButton:hover {
      background-color: #0056b3;
    }
  </style>
</head>

<body>
  <div class="bg" id="content">
    <!-- 身份证内容 -->
    <div class="ID-box absolute">
      <img class="user-img absolute" id="myImage" src="">
      <!-- 姓名 -->
      <div class="user-name absolute"></div>
      <!-- 性别 -->
      <div class="user-gender absolute"></div>
      <!-- 身份证号 -->
      <div class="user-idCard absolute"></div>
      <!-- 证书编号 -->
      <div class="certificate-number absolute"></div>
      <!-- 企业名称 -->
      <div class="company absolute"></div>
      <!-- 职务 -->
      <div class="post absolute"></div>
      <!-- 技术职称 -->
      <div class="technical-titles absolute"></div>
      <!-- 有效期至 -->
      <div class="effective-date absolute"></div>
      <div class="init-date"></div>
      <!-- 二维码 -->
      <div class="qr">
        <div id="qrcode"></div>
      </div>
    </div>
  </div>
  <!-- Export button -->
  <button id="exportButton">导出</button>
  <!-- Scripts -->
  <script>

  </script>
  <script>
    var queryString = window.location.search
    var urlParams = new URLSearchParams(queryString)
    var id = urlParams.get('id')
    var info = {
    }
    var name = ''
    fetch('http://api.ynjov.com:8080/api/getCertificatesDetail/' + id)
      .then(response => {
        if (!response.ok) {
          throw new Error('Network response was not ok')
        }
        return response.json()
      })
      .then(({ data }) => {
        info = data
        name = data.name
        info.name = name.trim()  // .replace(/(?<=.)./g, '*')
        fillContent()
      })
      .catch(error => {
        console.error('Fetch error:', error)
      })
    // 用于动态填充内容的函数
    function fillContent() {
      document.querySelector('.user-name').innerText = info.name
      document.querySelector('.user-gender').innerText = info.gender
      document.querySelector('.user-idCard').innerText = info.idNumber
      document.querySelector('.certificate-number').innerText = info.certificateNumber
      document.querySelector('.company').innerText = info.companyName
      document.querySelector('.post').innerText = info.position
      document.querySelector('.technical-titles').innerText = info.technicalTitle
      document.querySelector('.effective-date').innerText = info.validUntil
      document.querySelector('.init-date').innerText = info.initialCertificationDate

      // 设置用户头像
      var userImgElement = document.querySelector('.user-img')
      // 用 info 对象中的图片 URL 替换原来的图片链接
      userImgElement.src = `http://api.ynjov.com:8080/${info?.avatar}?t=${Date.now()}`
      userImgElement.setAttribute("crossOrigin", 'Anonymous')
    }

    // 获取当前页面的 URL
    var currentURL = `http://sys.ynjov.com/certresult.html?id=${id}` // 二维码

    // 创建 QRCode 实例并将二维码添加到页面
    var qrcode = new QRCode(document.getElementById("qrcode"), {
      text: currentURL,
      width: 126,
      height: 126
    })
    window.onload = function () {
      // if (urlParams.get('isMobile')) {
      //   document.getElementById("exportButton").hidden = true
      // }
    }
    // 获取要导出的HTML元素
    const element = document.getElementById('content')

    const pdf = new jsPDF()
    // 使用 html2pdf 将 HTML 元素导出为 PDF
    exportButton.addEventListener('click', () => {
      document.getElementById("exportButton").hidden = true
      window.document.title = `${name}B类证书`
      window.print()
      document.getElementById("exportButton").hidden = false
      window.document.title = `B类证书`
      return
    })
  </script>
</body>

</html>